﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.FIM;
using KPIS.GERP.Models.HRM;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Mvc;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.WCF.GFMS
{
    [ServiceContract]
    public interface IMasBankAccountService
    {
        [OperationContract]
        IEnumerable<KPIS.GERP.Models.FIM.MasBankAccount> SelectBankAccount(MasBankAccount criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int InsertBankAccount(MasBankAccount model, int createByUserId);

        [OperationContract]
        void UpdateBankAccount(MasBankAccount model, int updateByUserId);

        [OperationContract]
        void DeleteBankAccount(int id, int deleteByUserId);

        [OperationContract]
        IEnumerable<SelectListItem> SelectListWorkArea(MasWorkArea criteriaModel = null);

        [OperationContract]
        IEnumerable<SelectListItem> SelectListBankType(FimMasLookUp criteriaModel = null);
    }

    [ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class MasBankAccountService : IMasBankAccountService
    {
        GERPContext db = new GERPContext();

        public IEnumerable<KPIS.GERP.Models.FIM.MasBankAccount> SelectBankAccount(MasBankAccount criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasBankAccount> query = context.MasBankAccounts;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int InsertBankAccount(MasBankAccount model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {

                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.MasBankAccounts.Add(model);
                    context.SaveChanges();

                    return model.BankAccId;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateBankAccount(MasBankAccount model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteBankAccount(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasBankAccounts.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IEnumerable<SelectListItem> SelectListWorkArea(MasWorkArea criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasWorkArea> query = context.MasWorkAreas;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.Name, Value = model.WorkAreaId.ToString() };
                        list.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public IEnumerable<SelectListItem> SelectListBankType(FimMasLookUp criteriaModel = null)
        {
            var list = new List<SelectListItem>();

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<FimMasLookUp> query = context.FimMasLookUps;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.Name, Value = model.LookUpId.ToString() };
                        list.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }
    }
}
